/**
 * @author Giuseppe Mendola
 */
package sa4.persistence

import org.squeryl.KeyedEntity
import org.squeryl.SessionFactory
import org.squeryl.Session
import org.squeryl.adapters.H2Adapter
import scala.util.matching.Regex
import org.squeryl.adapters.H2Adapter
import java.sql.Timestamp
import java.sql.Date
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.dsl.ast.LogicalBoolean
import org.squeryl.annotations.OptionType

class Persistence {
  /**
   * Function that add a new user in the database
   * @param userName	a String that contain the user name of the user.
   * @param name		a String that contain the real name of the user.
   * @param email		a String that contain the email of the user.
   * @param pass		a String that contain the password of the user.
   * @param number		a String that contain the number of the user.
   * @return			a User that represent the instance added in the database.
   */
  def newUser(userName: String, name: String, email: String, pass: String, number: String) = {
    transaction {
      UsiAds.users.insert(new User(userName, name, email, pass, number))
    }
  }

  //adds a new advertisment to the database
  def headAdsInDB(header: String): Boolean = {
    for (a <- UsiAds.adverts) {
      if (a.header == header) return true
    }
    return false
  }

  /**
   * Function that add a new advert in the database if user exists in the database.
   * @param header 		a String with the header of the advert.
   * @param description a String that contain the description of the advert.
   * @param user 		a String that represent the name of the user that posted the advert.
   */
  def newAdvert(header: String, description: String, user: String) {
    var cnt = false
    transaction {
      val x = from(UsiAds.users)(a =>
        where(a.id === user)
          compute (count(a.id)))
      cnt = x.single.measures.intValue() >= 1
    }
    if (cnt) {
      transaction {
        UsiAds.adverts.insert(new Advertisment(new java.util.Date, header, description, user))
      }
    }
  }
  
  /**
   * Functio that will return all the users in the database.
   * @return	a org.Squeryl.Query containg all the users in the database.
   */
  def getUsers = {
    transaction {
      val allUsers = from(UsiAds.users)(a => select(a))
      allUsers
    }
  }
}

//Setting Relation User
class User(val userName: String, val name: String, val email: String, val pass: String, val number: String) {
  val id = userName
}

//Setting Relation Advertisment
class Advertisment(val date: java.util.Date, val header: String, val description: String, val user: String) extends KeyedEntity[Long] {
  def this() = this(new java.util.Date, "", "", "")
  val id = 0L

  /**
   * Function that get all the adverts of the users.
   * @param user	a User instance of the User relation.
   */
  def getAdsOfUser(user: User) {
    from(UsiAds.adverts)(a =>
      where(user.id === a.user)
        select (a))
  }

  /**
   * Function that get all the adverts.
   */
  def getAllAds() {
    from(UsiAds.adverts)(a =>
      select(a))
  }
}

//Schema of the database
object UsiAds extends org.squeryl.Schema {
  val users = table[User]
  val adverts = table[Advertisment]
}